﻿using System;
using System.Data;
using System.Text;
using System.Drawing;
using HtmlViewControl;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

namespace CustomControls
{
   public class HtmlPopup : ToolStripControlHost
   {
      private Size FClientSize;

      public readonly HtmlView HtmlView;

      public ContextMenuStrip Menu
      {
         get
         {
            return Owner as ContextMenuStrip;
         }
      }

      public HtmlPopup(ContextMenuStrip menu)
         : base(new Panel())
      {
         HtmlView = new HtmlView();
         HtmlView.Dock = DockStyle.None;
         HtmlView.Location = new Point(0, 5);
         Control.BackColor = Color.Transparent;
         Control.Controls.Add(HtmlView);
         menu.Items.Add(this);
      }

      public Size ClientSize
      {
         get
         {
            return FClientSize;
         }
         set
         {
            FClientSize = value;
            HtmlView.Size = FClientSize;
            Size = Owner.Size = new Size(FClientSize.Width + 16, FClientSize.Height + 16);
         }
      }

      protected override void OnOwnerChanged(EventArgs e)
      {
         Owner.AutoSize = false;
         Owner.Size = Size;
         if (Owner is ContextMenuStrip)
         {
            Menu.ShowImageMargin = false;
            Menu.Opened += (o, a) => { Focus(); };
         }
         base.OnOwnerChanged(e);
      }
   }
}
